package com.ruoyi.controller;

import java.util.ArrayList;
import java.util.List;
import java.util.stream.Collectors;
import javax.servlet.http.HttpServletResponse;

import com.ruoyi.domain.Article;
import com.ruoyi.domain.HomePage;
import com.ruoyi.service.ArticleService;
import com.ruoyi.service.IHomePageService;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.PutMapping;
import org.springframework.web.bind.annotation.DeleteMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import com.ruoyi.common.annotation.Log;
import com.ruoyi.common.core.controller.BaseController;
import com.ruoyi.common.core.domain.AjaxResult;
import com.ruoyi.common.enums.BusinessType;

import com.ruoyi.common.utils.poi.ExcelUtil;
import com.ruoyi.common.core.page.TableDataInfo;

/**
 * 首页轮播图Controller
 *
 * @author ruoyi
 * @date 2025-02-10
 */
@RestController
@RequestMapping("/home_page/home_page")
public class HomePageController extends BaseController
{
    @Autowired
    private IHomePageService homePageService;
    @Autowired
    private ArticleService articleService;
    /**
     * 查询首页轮播图列表
     */

    @GetMapping("/getNewLimit8")
    public AjaxResult getNewLimit8() {
        List<HomePage> list = homePageService.query()
                .orderByDesc("home_page_id")          // 根据 id降序排列
                .last("LIMIT 8")            // 获取最新 8 条记录
                .list();
        List<Long>articleIdList=list.stream().map(HomePage::getArticleId).collect(Collectors.toList());
        List<Article>articleList=articleService.query().in("article_id",articleIdList).list();
        return success(articleList);
    }

    /**
     * 查询首页轮播图列表
     */

    @GetMapping("/list")
    public TableDataInfo list(HomePage homePage)
    {
        startPage();
        List<HomePage> list = homePageService.selectHomePageList(homePage);
        return getDataTable(list);
    }

    /**
     * 导出首页轮播图列表
     */

    @Log(title = "首页轮播图", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    public void export(HttpServletResponse response, HomePage homePage)
    {
        List<HomePage> list = homePageService.selectHomePageList(homePage);
        ExcelUtil<HomePage> util = new ExcelUtil<HomePage>(HomePage.class);
        util.exportExcel(response, list, "首页轮播图数据");
    }

    /**
     * 获取首页轮播图详细信息
     */

    @GetMapping(value = "/{homePageId}")
    public AjaxResult getInfo(@PathVariable("homePageId") Long homePageId)
    {
        return success(homePageService.selectHomePageByHomePageId(homePageId));
    }

    /**
     * 新增首页轮播图
     */

    @Log(title = "首页轮播图", businessType = BusinessType.INSERT)
    @PostMapping
    public AjaxResult add(@RequestBody HomePage homePage)
    {
        return toAjax(homePageService.insertHomePage(homePage));
    }

    /**
     * 修改首页轮播图
     */

    @Log(title = "首页轮播图", businessType = BusinessType.UPDATE)
    @PutMapping
    public AjaxResult edit(@RequestBody HomePage homePage)
    {
        return toAjax(homePageService.updateHomePage(homePage));
    }

    /**
     * 删除首页轮播图
     */

    @Log(title = "首页轮播图", businessType = BusinessType.DELETE)
	@DeleteMapping("/{homePageIds}")
    public AjaxResult remove(@PathVariable Long[] homePageIds)
    {
        return toAjax(homePageService.deleteHomePageByHomePageIds(homePageIds));
    }
}
